<?php
/**
 * 统计中奖名单
 *
 * Date: 2017/8/3
 * Time: 17:13
 */
defined('InNcPHP') or exit('Access Invalid!');

class lotterynumberControl extends  Control {


    public $redis;

    public $deploy;


    public function stat_lotterynumberOp(){

        $row=20;
        $this->redis = new Redis();
        $this->deploy = C('redis');
        $this->redis->connect($this->deploy['master']['host'],$this->deploy['master']['port']);
        $order_model = Model('bargain_order');
        $lottery_model = Model('bargain_lottery');
        $last_month = date('Y-m', strtotime('last month'));
        $last['first'] = $last_month . '-01 00:00:00';
        $last['end'] = date('Y-m-d H:i:s', strtotime("$last_month +1 month -1 day +23 hours +59 minutes +59 seconds"));
        //$start_time = strtotime($last['first']);
        $end_time = strtotime($last['end']);
        //$condition = array();
        $pxname = $this->deploy['prefix'].'orderpx';
        $res  = $this->redis->zRevRange($pxname,0,$row);
        $order_id = substr($res[0],9);
        $order_model->updateBgOrder(array('bg_order_id'=>$order_id),array('if_winners'=>1,'winners_time'=>$end_time));//中奖名单
        //var_dump($res[0]);exit;
        $pk_list = array();
        foreach($res as $k){
            $order_id = substr($k,9);
            $pk_list[] = $this->redis->hGetAll($this->deploy['prefix'].'month_order'.$order_id);
        }
        foreach ($pk_list as $key =>$v){

            $lottery_model->addLottery(array(
                'order_id'=>$v['order_id'],
                'lottery_time'=>$end_time,
                'lottery_ym'=>date('Ym',$end_time),
                'nickname'=>$v['nickname'],
                'if_lottery'=>$key=='0'?1:0,
                'bg_title'=>$v['bg_title'],
                'in_all'=>$v['in_all'],

            ));
        }


    }


    /**
     * 统计当天的砍价活动信息
     */

    public  function  stat_bargain_infoOp(){

        //统计redis 缓存所有的订单砍价记录
        //统计订单总砍价次数
        //订单砍价总次数写入数据库
        $this->redis = new Redis();
        $this->deploy = C('redis');
        $this->redis->connect($this->deploy['master']['host'],$this->deploy['master']['port']);
        $pxname = $this->deploy['prefix'].'orderpx';
        $res  = $this->redis->zRevRange($pxname,0,-1);
        $order_model = Model('bargain_order');
        $model = Model('');
        $pk_list = array();
        foreach($res as $k){
            $order_id = substr($k,9);
            $pk_list[] = $this->redis->hGetAll($this->deploy['prefix'].'orders'.$order_id);
        }


        foreach($pk_list as $k =>$v){
            $order_model->updateBgOrder(array('bg_order_id'=>$v['order_id']),array('in_all'=>$v['in_all']));
        }

        //单个用户的砍价记录
        //单个用户砍价信息写入数据库
        $member_bargain = array();
        $data  = $this->redis->getKeys($this->deploy['prefix'].'orderu-'."*");
        foreach($data as $k){
            $order_id = substr($k,13);

            $member_bargain[] = $this->redis->hGetAll($this->deploy['prefix'].'orderu-'.$order_id);
        }

        foreach($member_bargain as $k=>$v){
            $model->table('bargain_count')->where(array('bg_order_id'=>$v['ba_order_id'],'member_id'=>$v['member_id']))->update(array('in_all'=>$v['in_all'],'true_all'=>$v['true_all']));
        }


        if(intval(date('d')) ===1){
            $this->stat_lotterynumberOp();
            $this->emptycacheOp();
        }

    }


    /**
     * 清空redis 缓存 （每个月末执行）
     */

    public  function emptycacheOp(){

        $this->redis = new Redis();
        $this->deploy = C('redis');
        $this->redis->connect($this->deploy['master']['host'],$this->deploy['master']['port']);
        $pxname = $this->deploy['prefix'].'orderpx';
        $monthordername= $this->deploy['prefix'].'month_order';
        $res  = $this->redis->zRevRange($pxname,0,-1);
        //删除上月砍价次数缓存表
        foreach($res as $k){
            $order_id = substr($k,9);
            $this->redis->del($monthordername.$order_id);//清除上月订单砍赞次数redis
            $this->redis->zRem($pxname,'order_id-'.$order_id);//清空上月PK榜单redis
        }
       //删除上月砍价PK榜
        //$this->redis->flushAll();

    }





}